<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Partners extends CI_Controller {

        function __construct()
        {
            parent::__construct();
            $this->load->model('partners_model');
        }

        public function index(){$this->view_all();}

        public function view_new()
        {
            $this->data = array(
                'title' => "New partner",
                'post_action' => "NEW",
            );
            $this->template->build('partners/edit_partner',$this->data); 
        }

        function view_edit($partner_ID){

            $this->data = array(
                'title' => "Edit partner",
                'post_action' => "EDIT",
                'post' => $this->partners_model->view_edit_partner($partner_ID)
            );
            $this->template->build('partners/edit_partner',$this->data);            
        }

        public function view_all()
        {
            $this->data = array(
                'title' => "Partners",
                'partners' => $this->partners_model->view_all_partners()
            );
            $this->template->build('partners/view_all_partners',$this->data);
        }

        public function submit_post()
        {
            /*General*/
            $this->form_validation->set_rules('company_person', 'company / person', 'required'); 
            $this->form_validation->set_rules('owner_type[]', 'type', 'required');

            //2 type validation for company and person owner
            switch ($this->input->post('company_person')) {
                case 'company':
                    $this->form_validation->set_rules('name', 'name', 'required');

                    break;
                case 'person':
                    $this->form_validation->set_rules('surname', 'surname', 'required');

                    break;
            } 
            $this->form_validation->set_rules('tax_type', 'tax type', 'callback_dropdown_check'); 
            $this->form_validation->set_rules('address', 'address', 'required');
            $this->form_validation->set_rules('city', 'city', 'required');
            $this->form_validation->set_rules('email', 'email', 'valid_email');       
            $this->form_validation->set_rules('telephone', 'telephone', 'required');       

            $this->form_validation->set_message('required','Please enter owner <b>%s</b>!');

            if($this->form_validation->run() == TRUE)
            {
                switch($_POST['post_action']){

                    case 'NEW':     $result = $this->partners_model->create_partner();
                        break;

                    case 'EDIT':    $result = $this->partners_model->update_partner();
                        break;

                }

                echo json_encode($result);   

            }
            else
            {
                echo json_encode(array('action'=>FALSE, 'msg'=>validation_errors()));
            }    

        }

        function delete($post_ID){

            if($this->partners_model->delete($post_ID)){
                $this->view_all();
            }else{
                echo 'Error ocured';
                //$this->view_all();
            }

        }

        function dropdown_check($str)
        {
            if ($str == '0')
            {
                $this->form_validation->set_message('dropdown_check', 'Please choose owner <b>%s</b>!');
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }
}